package DB.Dao.BreakDown;

import java.util.List;

import DB.Dao.BreakStaus.BreakStatus;
import DB.Dao.BreakStaus.BreakStatus.BreakStatusItem;
import DB.Dao.BreakStaus.BreakStatusDAO;
import DB.Dao.EnterPrisUser.EnterprisUser;
import DB.Dao.EnterPrisUser.EnterprisUserDAO;
import DB.Dao.Equipment.Equipment;
import DB.Dao.Equipment.EquipmentDAO;

/**
 * BreakDown entity. @author MyEclipse Persistence Tools
 */

public class BreakDown implements java.io.Serializable {

	// Fields

	private Integer breakId;
	private Integer userId;
	private Integer equipmentId;
	private String address;
	private String description;
	private Integer breakStatus;

	// Constructors

	/** default constructor */
	public BreakDown() {
	}

	/** minimal constructor */
	public BreakDown(Integer breakId) {
		this.breakId = breakId;
	}

	/** full constructor */
	public BreakDown(Integer breakId, Integer userId, Integer equipmentId,
			String address, String description, Integer breakStatus) {
		this.breakId = breakId;
		this.userId = userId;
		this.equipmentId = equipmentId;
		this.address = address;
		this.description = description;
		this.breakStatus = breakStatus;
	}

	// Property accessors

	public Integer getBreakId() {
		return this.breakId;
	}

	public void setBreakId(Integer breakId) {
		this.breakId = breakId;
	}

	public Integer getUserId() {
		return this.userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Integer getEquipmentId() {
		return this.equipmentId;
	}

	public void setEquipmentId(Integer equipmentId) {
		this.equipmentId = equipmentId;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getBreakStatus() {
		return this.breakStatus;
	}

	public void setBreakStatus(Integer breakStatus) {
		this.breakStatus = breakStatus;
	}
	public void add2DB(EnterprisUser user,Equipment equipment) throws Exception{
		
		EquipmentDAO equipDao=new EquipmentDAO();
		List list=equipDao.findByExample(equipment);
		//equipDao.delete(equipment);
		if(list.size()==0){
			System.out.println("size==0");
			equipDao.attachDirty(equipment);
		}
		else{
			System.out.println("sieze!=0");
			equipment=(Equipment) list.get(0);
		}

		
		EnterprisUserDAO userDao=new EnterprisUserDAO();
		list=userDao.findByExample(user);
		if(list.size()==0){
			throw new Exception("user error");
		}
		user=(EnterprisUser)list.get(0);
		
		
		
		 BreakStatusDAO statusDao=new BreakStatusDAO();
		 
		 BreakStatus status=(BreakStatus)( statusDao.findByStatusName("新故障").get(0));
		
		
		
		BreakDownDAO dao=new BreakDownDAO();
		BreakDown ins=new BreakDown();

		ins.setEquipmentId(equipment.getEquipmentId());
		ins.setUserId(user.getUserId());
		ins.setBreakStatus(status.getStatusId());
		dao.save(ins);
	}
	public void up2DB(){
		 BreakDownDAO statusDao=new BreakDownDAO();
		 statusDao.attachDirty(this);
	}
	public void setOnWorking(){
		this.setBreakStatus(BreakStatusItem.Working.id);
		up2DB();
	}
	public void setDone(){
		this.setBreakStatus(BreakStatusItem.Done.id);
		up2DB();
	}
	
	

}